<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    //52.13%
    // var sortedSquares = function(A) {
    //     for(let i in A){
    //         A[i] = A[i]*A[i];
    //     }
    //     A.sort(function(a,b){
    //         return a-b;
    //     })
    //     return A;
    // };

    //99.78%
    var sortedSquares = function(A) {
        const len = A.length;
        let i=0, j=len-1, arr = [];
        while(i<=j){
            if(Math.abs(A[i]) >= Math.abs(A[j])){
                arr.push(A[i]*A[i]);
                i++;
            }else{
                arr.push(A[j]*A[j]);
                j--;
            }
        }
        return arr.reverse();
    }
    let A = [-4,-1,0,3,10];
    console.log(sortedSquares(A));
    
</script>
</html>